#include "tim.h"

sbit OUT = P1^4;

void Timer0Init(void)		
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	
	TL0 = (65536 - SYSCLK_12/(DEF_T0_OUT_FREQ << 1)) & 0xFF;		//设置定时初始值
	TH0 = (65536 - SYSCLK_12/(DEF_T0_OUT_FREQ << 1)) >> 8;		//设置定时初始值
	
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}


void Timer1Init(void)		//1毫秒@22.1184MHz
{
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x9A;		//设置定时初始值
	TH1 = 0xA9;		//设置定时初始值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
}

void Timer_Init(void)
{
	Timer0Init();
	Timer1Init();
	ET0 = 1;
	ET1 = 1;
}


void SetPulseFreq(uint16_t freq)
{
	TL0 = (65536 - SYSCLK_12/(freq << 1)) & 0xFF;		//设置定时初始值
	TH0 = (65536 - SYSCLK_12/(freq << 1)) >> 8;		//设置定时初始值
}


void TM0_Isr() interrupt 1
{
	OUT = ~OUT;
}

void TM1_Isr() interrupt 3
{
	task1++;
	task2++;
	if(uart1_Idle & 0x80){
		uart1_Idle++;
		if(uart1_Idle >= 0x85){
			rx1_buf[rx1_sta] = '\0';
			rx1_sta |= 0x80;
			uart1_Idle = 0;
		}
	}
}
